package craky.util;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.abigdreamer.swing.component.UIUtil;

public class WindowMove extends MouseAdapter
{
  private Point point;
  private Window window;
  private Component component;
  private boolean moveable;

  public WindowMove(Component component)
  {
    this.moveable = true;
    this.point = new Point(-1, -1);
    changeComponent(component);
  }

  public void changeComponent(Component component)
  {
    if (this.component != null)
    {
      this.component.removeMouseMotionListener(this);
      this.component.removeMouseListener(this);
    }

    this.component = component;
    this.window = UIUtil.getWindowFromComponent(component);

    if (this.window != null)
    {
      component.addMouseMotionListener(this);
      component.addMouseListener(this);
    }
  }

  public boolean isMoveable()
  {
    return this.moveable;
  }

  public void setMoveable(boolean moveable)
  {
    this.moveable = moveable;
  }

  public void mouseDragged(MouseEvent e)
  {
    boolean move = this.moveable;

    if ((move) && ((this.window instanceof Frame)))
    {
      move = (((Frame)this.window).getExtendedState() & 0x6) == 0;
    }

    if ((move) && (this.point.x >= 0) && (this.point.y >= 0))
    {
      int x = e.getXOnScreen() - this.point.x;
      int y = e.getYOnScreen() - this.point.y;
      this.window.setLocation(x, y);
    }
  }

  public void mousePressed(MouseEvent e)
  {
    if (this.moveable)
    {
      if (e.getButton() == 1)
      {
        this.point.move(e.getXOnScreen() - this.window.getX(), e.getYOnScreen() - this.window.getY());
      }
      else
      {
        this.point.move(-1, -1);
      }
    }
  }

  public void mouseReleased(MouseEvent e)
  {
    if ((this.moveable) && (e.getButton() == 1))
    {
      this.point.move(-1, -1);
    }
  }
}